<template>
<div style="height: 6vh; background-color: #fff; padding: 0 20px 10px;">
  <span style="font-size: 20px; line-height: 60px;">用户管理</span>
  <el-button type="primary" style="float: right; margin-top: 12px;"
  @click="beforeAddUser">新建用户</el-button>
</div>
<el-dialog :title="dialogTitle" style="width: 1000px; padding: 40px;" v-model="dialogVisible" :before-close="handleClose">
  <el-form label-position="top">
    <el-row :gutter="10">
      <el-col :span="12">
        <el-form-item label="用户名"><el-input placeholder="请输入用户名" v-model="userSaveForm.username"></el-input></el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号"><el-input placeholder="请输入手机号" v-model="userSaveForm.phone"></el-input></el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="12">
        <el-form-item label="邮箱"><el-input placeholder="请输入邮箱" v-model="userSaveForm.email"></el-input></el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="年龄"><el-input placeholder="请输入年龄" v-model="userSaveForm.age"></el-input></el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="6">
        <el-form-item label="职级">
          <el-select placeholder="请选择" v-model="userSaveForm.level" @change="loadLeader">
            <el-option label="员工" value="10"></el-option>
            <el-option label="经理" value="20"></el-option>
            <el-option label="总监" value="30"></el-option>
            <el-option label="总裁" value="40"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="直属领导">
          <el-select placeholder="请选择" v-model="userSaveForm.parentId">
            <el-option v-for="it in leaderOptions" :label="it.username" :value="it.id"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="4">
        <el-form-item label="性别">
          <el-radio-group  v-model="userSaveForm.gender">
            <el-radio border label="男" value="1" style="margin: 0;"></el-radio>
            <el-radio border label="女" value="0"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="5">
        <el-form-item label="状态">
          <el-radio-group  v-model="userSaveForm.status">
            <el-radio border label="启用" value="1" style="margin: 0;"></el-radio>
            <el-radio border label="禁用" value="0"></el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <template #footer>
    <el-button type="primary" @click="saveUser">确定</el-button>
    <el-button @click="handleClose">取消</el-button>
  </template>
</el-dialog>
<el-card style="margin: 10px 0;height: 70px;">
  <el-form :inline="true">
    <el-form-item label="用户搜索" style="width: 270px;">
      <el-input v-model="searchUserForm.username" placeholder="请输入用户名" />
    </el-form-item>
    <el-form-item label="用户状态" style="width: 270px;">
      <el-select v-model="searchUserForm.status" placeholder="请选择状态">
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="loadUser">查询</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </el-form-item>
  </el-form>
</el-card>
<el-card style="margin: 10px 0;">
  <el-table :data="userArr" style="width: 100%">
      <el-table-column type="index" label="序号" width="60" align="center"/>
      <el-table-column prop="username" label="名称" align="center"/>
      <el-table-column prop="phone" label="手机号" align="center"/>
      <el-table-column prop="createTime" label="加入时间" align="center"/>
      <el-table-column label="用户状态" align="center">
        <template #default="scope">
          <el-switch v-model="scope.row.status" inactive-value="0" active-value="1" @change="changeStatus(scope.row.id,scope.row.status)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="resetPassword(scope.row.id)" :disabled="scope.row.level == 40">密码重置</el-button>
          <el-button type="primary" link @click="editUser(scope.row.id)" :disabled="scope.row.level == 40">编辑</el-button>
          <el-button type="primary" link @click="deleteUser(scope.row.id)" :disabled="scope.row.level == 40">删除</el-button>
        </template>
      </el-table-column>
  </el-table>
</el-card>
</template>

<script setup>
import { ref,onMounted } from 'vue';
import qs from 'qs';
import axios from 'axios';
import { ElMessage } from 'element-plus';

const dialogVisible = ref(false);
const leaderOptions = ref([]);
const userSaveForm = ref({
  username: '',
  phone: '',
  email: '',
  age: '',
  level: '',
  parentId: '',
  gender: '',
  status: ''
});
const searchUserForm = ref({
  username: '',
  status: ''
});
const userArr = ref([]);
const dialogTitle = ref('');
onMounted(() => {
  loadUser();
});
const saveUser = () => {
  console.log(userSaveForm.value);
  let data = qs.stringify(userSaveForm.value);
  axios.post(BASE_URL + '/v1/user/save',data).then(res=>{
    if(res.data.code == 2000) {
      ElMessage.success('保存成功');
      userSaveForm.value = {};
      dialogVisible.value = false;
      loadUser();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const handleClose = () => {
  if(confirm('确定取消保存用户吗')){
    userSaveForm.value = {};
    dialogVisible.value = false;
    leaderOptions.value = [];
  }
};
const loadUser = () => {
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASE_URL + '/v1/user/list?' + data).then(res=>{
    if(res.data.code == 2000) {
      userArr.value = res.data.data;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
}
const resetSearch = () => {
  searchUserForm.value = {};
  loadUser();
};
const loadLeader = () => {
  userSaveForm.value.parentId = '';
  leaderOptions.value = [];
  let level = parseInt(userSaveForm.value.level) + 10;
  axios.get(BASE_URL + '/v1/user/list?level=' + level).then(res => {
    if(res.data.code == 2000){
      leaderOptions.value = res.data.data;
    } else{
      ElMessage.error(res.data.msg);
    }
  })
};
const beforeAddUser = () => {
  dialogVisible.value = true;
  dialogTitle.value = '新建用户';
};
const editUser = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑用户';
  axios.get(BASE_URL + '/v1/user/list?id='+ id).then(res => {
    if(res.data.code == 2000) {
      userSaveForm.value = res.data.data[0];
      let level = parseInt(userSaveForm.value.level) + 10;
      axios.get(BASE_URL + '/v1/user/list?level=' + level).then(response => {
        if(response.data.code == 2000){
          leaderOptions.value = response.data.data;
        } else{
          ElMessage.error(response.data.msg);
        }
      });
    } else {
      ElMessage.error(res.data.msg);
    }
  })
};
const resetPassword = (id) => {
  axios.post(BASE_URL + '/v1/user/update/password/' + id).then(response=>{
    if(response.data.code == 2000) {
      ElMessage.success("重置密码成功！");
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};
const changeStatus = (id,status) => {
  axios.post(BASE_URL + '/v1/user/update/status/' + id + '/' + status).then(response=>{
    if(response.data.code == 2000) {
      ElMessage.success("修改状态成功！");
    } else {
      ElMessage.error(response.data.msg);
    }
  })
};
const deleteUser = (id) => {
  if(confirm('确定删除该用户吗')){
    axios.post(BASE_URL + '/v1/user/delete/' + id).then(response=>{
      if(response.data.code == 2000) {
        ElMessage.success("删除成功！");
        loadUser();
      } else {
        ElMessage.error(response.data.msg);
     }
    })
  }
};
</script>

<style scoped>

</style>